package cn.itcast.bos.controller;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.domain.common.ResponseResult;
import cn.itcast.bos.service.CourierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.*;
import java.util.*;

/**
 * Created by Administrator on 2018/12/24.
 */
@RestController
@RequestMapping("/courier")
public class CourierController {
    // 注入Service
    @Autowired
    private CourierService courierService;

    // 添加快递员方法
    @RequestMapping("save")
    public ResponseResult save(Courier courier) {

        try {
            courierService.save(courier);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.FAIL();
        }

        return ResponseResult.SUCCESS();
    }

    // 分页查询方法
    @RequestMapping("pageQuery")
    public Map pageQuery(int page, int rows,Courier courier) {
        // 封装Pageable对象
        Pageable pageable = PageRequest.of(page-1,rows);
        //创建查询条件
        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            // Root 用于获取属性字段，CriteriaQuery可以用于表达式条件查询，CriteriaBuilder 用于面向对象条件查询
            public Predicate toPredicate(Root<Courier> root,
                                         CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();

                // 简单单表查询
                if (StringUtils.isNotBlank(courier.getCourierNum())) {
                    Predicate p1 = cb.equal(
                            root.get("courierNum").as(String.class),
                            courier.getCourierNum());
                    list.add(p1);
                }
                if (StringUtils.isNotBlank(courier.getCompany())) {
                    Predicate p2 = cb.like(
                            root.get("company").as(String.class),
                            "%" + courier.getCompany() + "%");
                    list.add(p2);
                }
                if (StringUtils.isNotBlank(courier.getType())) {
                    Predicate p3 = cb.equal(root.get("type").as(String.class),
                            courier.getType());
                    list.add(p3);
                }
                // 多表查询
                Join<Courier, Standard> standardJoin = root.join("standard",
                        JoinType.INNER);
                if (courier.getStandard() != null
                        && StringUtils.isNotBlank(courier.getStandard()
                        .getName())) {
                    Predicate p4 = cb.like(
                            standardJoin.get("name").as(String.class), "%"
                                    + courier.getStandard().getName() + "%");
                    list.add(p4);
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };

        // 调用业务层 ，返回 Page
        Page<Courier> pageData = courierService.findPageData(specification,
                pageable);
        // 将返回page对象 转换datagrid需要格式
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageData.getTotalElements());
        result.put("rows", pageData.getContent());


        return result;
    }


    // 作废快递员
    @RequestMapping("delBatch")
    public ResponseResult delBatch(String ids) {
        try {
            // 按,分隔ids
            String[] idArray = ids.split(",");
            // 调用业务层，批量作废
            courierService.delBatch(idArray);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.FAIL();
        }

        return ResponseResult.SUCCESS();
    }

    // 还原快递员
    @RequestMapping("backBatch")
    public ResponseResult backBatch(String ids) {
        try {
            // 按,分隔ids
            String[] idArray = ids.split(",");
            // 调用业务层，批量还原
            courierService.backBatch(idArray);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.FAIL();
        }

        return ResponseResult.SUCCESS();
    }
}
